package com.zyb.beiojbackendjudgeservice.codesandbox.impl;


import cn.hutool.core.bean.BeanUtil;
import com.zyb.beiojbackendjudgeservice.codesandbox.CodeSandbox;
import com.zyb.beiojbackendserviceclient.service.UserFeignClient;
import com.zyb.beiojclientsdk.BeiojApiClientConfig;
import com.zyb.beiojclientsdk.client.BeiojCodeSandboxClient;


import com.zyb.beiojclientsdk.domain.CodeSandboxRequest;
import com.zyb.beiojclientsdk.domain.CodeSandboxResponse;
import com.zyb.beiojmodel.model.codesandbox.ExecuteCodeRequest;
import com.zyb.beiojmodel.model.codesandbox.ExecuteCodeResponse;
import com.zyb.beiojmodel.model.codesandbox.QuestionJudgeInfo;
import com.zyb.beiojmodel.model.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author 十八岁讨厌编程
 * @date 2025/2/8 10:01
 * @PROJECT_NAME beioj-backend
 * @description 远程代码沙箱（有java原生、docker两种实现方式）
 */


@Slf4j
@Service
public class RemoteCodeSandbox implements CodeSandbox {

    @Resource
    private UserFeignClient userService;


    public ExecuteCodeResponse executeCode(ExecuteCodeRequest executeCodeRequest) {
        log.info("remote");
        //使用sdk调用代码沙箱服务
        //构造请求体
        CodeSandboxRequest codeSandboxRequest = new CodeSandboxRequest();
        BeanUtil.copyProperties(executeCodeRequest,codeSandboxRequest);
        log.info("sdk请求体" + codeSandboxRequest);
        //拿到用户的ak sk用于鉴权
        User user = userService.getById(executeCodeRequest.getUserId());
        String accessKey = user.getAccessKey();
        String secretKey = user.getSecretKey();
        BeiojCodeSandboxClient beiojCodeSandboxClient = new BeiojCodeSandboxClient(accessKey,secretKey);
        CodeSandboxResponse codeSandboxResponse = beiojCodeSandboxClient.callRemoteCodeSandbox(codeSandboxRequest);
        //构造返回体
        QuestionJudgeInfo questionJudgeInfo = new QuestionJudgeInfo();
        BeanUtil.copyProperties(codeSandboxResponse.getJudgeInfo(),questionJudgeInfo);
        ExecuteCodeResponse executeCodeResponse = new ExecuteCodeResponse();
        BeanUtil.copyProperties(codeSandboxResponse,executeCodeResponse);
        executeCodeResponse.setJudgeInfo(questionJudgeInfo);
        log.info("sdk返回体" + codeSandboxResponse);
        return executeCodeResponse;
    }
}
